package cn.ytian.scashier;

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Enumeration;
import java.util.Properties;

public class Config {

    private static final String ConfigPath = "src/config/Cof.properties";
    private String Language = "cn";
    public static String User = "Ytian";
    public String Password = "YtianNb";

    public static String DataType = "dat";
    public static String ProductPathDat = "src/Data/ProductPathDat.dat";
    public static String SalesItemPathDat = "src/Data/SalesItemPathDat.dat";
    public static String OrderItemPathDat = "src/Data/OrderItemPathDat.dat";
    public static String JDBC_url;
    public static String JDBC_post;
    public static String JDBC_database;
    public static String JDBC_user;
    public static String JDBC_password;

    public String getLanguage() {
        return this.Language;
    }

    public void setLanguage(String language) {
        this.Language = language;
        try {
            WriteProperties("Language", language);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getUser() {
        return this.User;
    }

    public void setUser(String user) {
        this.User = user;
        try {
            WriteProperties("User", user);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public String getPassword() {
        return this.Password;
    }

    public void setPassword(String password) {
        this.Password = password;
        try {
            WriteProperties("Password", password);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //根据Key读取Value
    public static String GetValueByKey(String key) {
        return GetValueByKey(ConfigPath, key);
    }

    //根据Key读取Value
    public static String GetValueByKey(String path, String key) {
        Properties pps = new Properties();
        try {
            // InputStream in = new BufferedInputStream(new FileInputStream(ConfigPath));
            InputStreamReader in = new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8);
            pps.load(in);
            //System.out.println(key + " = " + value);
            return pps.getProperty(key);

        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    //读取Properties的全部信息
    public static void GetAllProperties(String path) throws IOException {

        Properties pps = new Properties();
        //InputStream in = new BufferedInputStream(new FileInputStream(path));
        InputStreamReader in2 = new InputStreamReader(new FileInputStream(path), StandardCharsets.UTF_8);
        pps.load(in2);
        Enumeration<?> en = pps.propertyNames(); //得到配置文件的名字

        while (en.hasMoreElements()) {
            String strKey = (String) en.nextElement();
            String strValue = pps.getProperty(strKey);
            System.out.println(strKey + "=" + strValue);
        }
    }

    //写入Properties信息
    public static void WriteProperties(String pKey, String pValue) throws IOException {
        Properties pps = new Properties();
        InputStream in = new FileInputStream(ConfigPath);
        //从输入流中读取属性列表（键和元素对）
        pps.load(in);
        //调用 Hashtable 的方法 put。使用 getProperty 方法提供并行性。
        //强制要求为属性的键和值使用字符串。返回值是 Hashtable 调用 put 的结果。
        OutputStream out = new FileOutputStream(ConfigPath);
        pps.setProperty(pKey, pValue);
        //以适合使用 load 方法加载到 Properties 表中的格式，
        //将此 Properties 表中的属性列表（键和元素对）写入输出流
        pps.store(out, "Update " + pKey + " name");
    }

    public void load() {
        Language = GetValueByKey("Language") != null ? GetValueByKey("Language") : Language;
        User = GetValueByKey("User") != null ? GetValueByKey("User") : User;
        Password = GetValueByKey("Password") != null ? GetValueByKey("Password") : Password;
        DataType = GetValueByKey("DataType") != null ? GetValueByKey("DataType") : DataType;

        ProductPathDat = GetValueByKey("ProductPathDat") != null ? GetValueByKey("ProductPathDat") : ProductPathDat;
        SalesItemPathDat = GetValueByKey("SalesItemPathDat") != null ? GetValueByKey("SalesItemPathDat") : SalesItemPathDat;

        JDBC_url = GetValueByKey("JDBC.url");
        JDBC_post = GetValueByKey("JDBC.post");
        JDBC_database = GetValueByKey("JDBC.database");
        JDBC_user = GetValueByKey("JDBC.user");
        JDBC_password = GetValueByKey("JDBC.password");

    }

    public Config() throws IOException {
        YLib.inFile(ConfigPath);
        load();
        save();
    }

    public void save() throws IOException {
        WriteProperties("Language", this.Language);
        WriteProperties("User", this.User);
        WriteProperties("Password", this.Password);
        WriteProperties("DataType", DataType);
        WriteProperties("ProductPathDat", ProductPathDat);
        WriteProperties("SalesItemPathDat", SalesItemPathDat);
    }
}
